package com.frxuexi.cart.viewmodel

import com.frxuexi.lib_base.entity.AddressBean
import com.frxuexi.lib_base.entity.AddressBeanItem
import com.frxuexi.lib_base.entity.CartBean
import com.frxuexi.lib_base.entity.CommentAddReqBean
import com.frxuexi.lib_base.entity.CouponBean
import com.frxuexi.lib_base.entity.CreateOrderBean
import com.frxuexi.lib_base.entity.CreateOrderReqBean
import com.frxuexi.lib_base.entity.EditAddressReq
import com.frxuexi.lib_base.entity.MyCouponBean
import com.frxuexi.lib_base.entity.OrderBean
import com.frxuexi.lib_base.entity.OrderDetailBean
import com.frxuexi.lib_base.entity.OutOrderBean
import com.frxuexi.lib_base.entity.PayOrderReqBean
import com.frxuexi.lib_base.http.ApiService
import com.frxuexi.lib_base.mvvm.BaseViewModel
import com.frxuexi.lib_base.room.ProductDao
import com.frxuexi.lib_base.room.ProductEntity
import com.frxuexi.lib_base.utils.SignLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class CartViewModel @Inject constructor(val apiService: ApiService,val productDao: ProductDao):BaseViewModel() {

    val cartError=SignLiveData<String>()
    val cartData=SignLiveData<CartBean>()
    fun getCarts(page:Int){
        httpRequest({apiService.getCarts(page)},cartData::postValue,cartError::postValue)
    }

    val upadteCartData=SignLiveData<String>()
    fun upadteCart(id:Int,num:Int){
        httpRequest({apiService.updateCart(id,num)},upadteCartData::postValue,cartError::postValue)
    }

    val deleteCartData=SignLiveData<String>()
    fun deleteCart(ids:List<Int>){
        httpRequest({apiService.deleteCart(ids)},deleteCartData::postValue,cartError::postValue)
    }

    val addProData=SignLiveData<Long>()
    fun addPro(productEntity: ProductEntity){
        roomRequest({productDao.addPro(productEntity)},addProData::postValue,cartError::postValue)
    }

    val couponsData=SignLiveData<CouponBean>()
    fun getCoupons(){
        httpRequest({apiService.getCoupons()},couponsData::postValue,cartError::postValue)
    }

    val myCouponData=SignLiveData<MyCouponBean>()
    fun getMyCoupon(){
        httpRequest({apiService.getMyCoupon()},myCouponData::postValue,cartError::postValue)
    }

    val defaultAddressData=SignLiveData<AddressBeanItem>()
    fun getDefaultAddress(){
        httpRequest({apiService.getDefaultAddress()},defaultAddressData::postValue,cartError::postValue)
    }

    val addressData=SignLiveData<AddressBean>()
    fun getAddress(){
        httpRequest({apiService.getAddress()},addressData::postValue,cartError::postValue)
    }

    val editAddressData=SignLiveData<AddressBeanItem>()
    fun editAddress(bean:EditAddressReq){
        httpRequest({apiService.editAddress(toBody(bean))},editAddressData::postValue,cartError::postValue)
    }

    val addressByIdData=SignLiveData<AddressBeanItem>()
    fun getAddressById(id:Int){
        httpRequest({apiService.getAddressById(id)},addressByIdData::postValue,cartError::postValue)
    }

    val deleteAddressData=SignLiveData<String>()
    fun deleteAddress(map:Map<String,Int>){
        httpRequest({apiService.deleteAddress(toBody(map))},deleteAddressData::postValue,cartError::postValue)
    }

    val createOrderData=SignLiveData<CreateOrderBean>()
    fun createOrder(item:CreateOrderReqBean){
        httpRequest({apiService.createOrder(toBody(item))},createOrderData::postValue,cartError::postValue)
    }

    val orderData=SignLiveData<OrderBean>()
    fun getOrders(page: Int,type:String){
        httpRequest({apiService.getOrders(page, type)},orderData::postValue,cartError::postValue)
    }

    val payOrderData=SignLiveData<Any>()
    fun payOrder(bean:PayOrderReqBean){
        httpRequest({apiService.payOrder(toBody(bean))},payOrderData::postValue,cartError::postValue)
    }
    val orderByIdData=SignLiveData<OrderDetailBean>()
    fun getOrderById(oid:String){
        httpRequest({apiService.getOrderById(oid)},orderByIdData::postValue,cartError::postValue)
    }

    val cancelOrderData=SignLiveData<Any>()
    fun cancelOrder(id:Int){
        httpRequest({apiService.cancelOrder(id)},cancelOrderData::postValue,cartError::postValue)
    }

    val addCommentData=SignLiveData<Any>()
    fun addComment(bean:CommentAddReqBean){
        httpRequest({apiService.addComment(toBody(bean))},addCommentData::postValue,cartError::postValue)
    }

    val outOrderData=SignLiveData<Any>()
    fun outOrder(map: Map<String, Any>){
        httpRequest({apiService.outOrder(toBody(map))},outOrderData::postValue,cartError::postValue)
    }
}